#! /usr/bin/python3
# -*- coding: utf-8 -*-
# __author__ = "Fengxuewei"
# Date: 2020/6/23

#1、某随机的序列中，计算出每个元素出现的频度，并找出频度出现最高的3个元素
#===================================================================================================================

from random import randint
data = [randint(0, 10) for _ in range(30) ]
print(data)

c = dict.fromkeys(data, 0)
print(c)

for x in data:
    c[x]+= 1
print(c)

# 传统的排序的方式
print(c.items())

# 字典的key以列表形式输出
print([key for key, value in c.items()])

# 字典的value以列表的形式输出
print([value for key, value in c.items()])

# 字典的key:value互换位置
print({value:value for key , value in c.items()})

# 字典按value的值进行排序（正序）
print(sorted(c.items(), key=lambda x: x[1]))


# 通过Collections库中的counter来排序
#===========================================================================================================


from collections import Counter
c1= Counter(c)
print(c1)

print(c1.most_common(3))

#2、对某个文本文件中的词出现的频度统计
#===================================================================================================================

import re
txt = open('C:\gmreadme.txt').read()
print(txt)

l1= re.split('\W+',txt)
print(l1)

c = Counter(l1)
print(c)

print(c.most_common(10))
